home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / ftpsubr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  4.0 KB  |  196 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "socket.h"
  5. #include "proc.h"
  6. #include "ftp.h"
  7. #include "ftpcli.h"
  8.  
  9. /* Send a file (opened by caller) on a network socket.
  10.  * Normal return: count of bytes sent
  11.  * Error return: -1
  12.  */
  13. long
  14. sendfile(fp,s,mode,hash)
  15. FILE *fp;    /* File to be sent */
  16. int s;        /* Socket to be sent on */
  17. int mode;    /* Transfer mode */
  18. int hash;    /* Print hash marks every BLKSIZE bytes */
  19. {
  20.     struct mbuf *bp;
  21.     int c, oldf;
  22.     long total = -1, hmark = 0;
  23.  
  24.     switch(mode){
  25.     default:
  26.     case LOGICAL_TYPE:
  27.     case IMAGE_TYPE:
  28.         sockmode(s,SOCK_BINARY);
  29.         for(;;){
  30.             bp = ambufw(BLKSIZE);
  31.             if((bp->cnt = fread(bp->data,1,BLKSIZE,fp)) == 0){
  32.                 free_p(bp);
  33.                 break;
  34.             }
  35.             total += bp->cnt;
  36.             if(send_mbuf(s,bp,0,NULLCHAR,0) == -1){
  37.                 return -1;
  38.             }
  39.             while(hash == V_HASH && total >= hmark+1000){
  40.                 tputc('#');
  41.                 hmark += 1000;
  42.             }
  43.             while(hash == V_BYTE && total >= hmark+1000){
  44.                 tprintf("Bytes sent: %ld\n", total);
  45.                 hmark += 1000;
  46.             }
  47.         }
  48.         break;
  49.     case ASCII_TYPE:
  50.         oldf = setflush(s,-1);
  51.         /* Let the newline mapping code in usputc() do the work */
  52.         sockmode(s,SOCK_ASCII);
  53.         while((c = getc(fp)) != EOF){
  54. #if !defined(UNIX) && !defined(__TURBOC__) && !defined(AMIGA)
  55.             if(c == '\r'){
  56.                 /* Needed only if the OS uses a CR/LF
  57.                  * convention and getc doesn't do
  58.                  * an automatic translation
  59.                  */
  60.                 continue;
  61.             }
  62. #endif
  63.             if(usputc(s,(char)c) == -1){
  64.                 total = -1;
  65.                 break;
  66.             }
  67.             total++;
  68.             while(hash == V_HASH && total >= hmark+1000){
  69.                 tputc('#');
  70.                 hmark += 1000;
  71.             }
  72.             while(hash == V_BYTE && total >= hmark+1000){
  73.                 tprintf("Bytes sent: %ld\n", total);
  74.                 hmark += 1000;
  75.             }
  76.         }
  77.         usflush(s);
  78.         setflush(s,oldf);
  79.         break;
  80.     }
  81.     if(hash)
  82.         tputc('\n');
  83.     return total;
  84. }
  85.  
  86. /* Receive a file (opened by caller) from a network socket.
  87.  * Normal return: count of bytes received
  88.  * Error return: -1
  89.  */
  90. long
  91. recvfile(fp,s,mode,hash)
  92. FILE *fp;
  93. int s;
  94. int mode;
  95. int hash;
  96. {
  97.     struct mbuf *bp;
  98.     int c, cnt;
  99.     long total, hmark;
  100.  
  101.     total = hmark = 0;
  102.  
  103.     switch(mode){
  104.     default:
  105.     case LOGICAL_TYPE:
  106.     case IMAGE_TYPE:
  107.         sockmode(s,SOCK_BINARY);
  108.         while((cnt = recv_mbuf(s,&bp,0,NULLCHAR,0)) != 0){
  109.             if(cnt == -1)
  110.                 return -1;
  111.             total += cnt;
  112.             while(hash == V_HASH && total >= hmark+1000){
  113.                 tputc('#');
  114.                 hmark += 1000;
  115.             }
  116.             while(hash == V_BYTE && total >= hmark+1000){
  117.                 tprintf("Bytes recv: %ld\n", total);
  118.                 hmark += 1000;
  119.             }
  120.             if(fp != NULLFILE){
  121.                 if(write_p(fp,bp) == -1){
  122.                     free_p(bp);
  123.                     return -1;
  124.                 }
  125.                 free_p(bp);
  126.             } else {
  127.                 send_mbuf(Curproc->output, bp, 0, NULLCHAR, 0);
  128.             }
  129.         }
  130.         break;
  131.     case ASCII_TYPE:
  132.         sockmode(s,SOCK_ASCII);
  133.         while((c = recvchar(s)) != EOF){
  134.             if(fp != NULLFILE){
  135. #if !defined(UNIX) && !defined(__TURBOC__) && !defined(AMIGA)
  136.                 if(c == '\n'){
  137.                     /* Needed only if the OS uses a CR/LF
  138.                      * convention and putc doesn't do
  139.                      * an automatic translation
  140.                      */
  141.                     putc('\r',fp);
  142.                 }
  143. #endif
  144.                 if(putc(c,fp) == EOF) {
  145.                     total = -1;
  146.                     break;
  147.                 }
  148.             } else {
  149.                 tputc((char)c);
  150.             }
  151.             total++;
  152.             while(hash == V_HASH && total >= hmark+1000){
  153.                 tputc('#');
  154.                 hmark += 1000;
  155.             }
  156.             while(hash == V_BYTE && total >= hmark+1000){
  157.                 tprintf("Bytes recv: %ld\n", total);
  158.                 hmark += 1000;
  159.             }
  160.         }
  161.         /* Detect an abnormal close */
  162.         if(socklen(s,0) == -1)
  163.             total = -1;
  164.         break;
  165.     }
  166.     if(hash)
  167.         tputc('\n');
  168.     return total;
  169. }
  170.  
  171. /* Determine if a file appears to be binary (i.e., non-text).
  172.  * Return 1 if binary, 0 if ascii text after rewinding the file pointer.
  173.  *
  174.  * Used by FTP to warn users when transferring a binary file in text mode.
  175.  */
  176. int
  177. isbinary(fp)
  178. FILE *fp;
  179. {
  180.     int c, i, rval;
  181.  
  182.     for(i = 0, rval = 0; i < 512; i++){
  183.         if((c = getc(fp)) == EOF)
  184.             break;
  185.         if(c & 0x80){
  186.             /* High bit is set, probably not text */
  187.             rval = 1;
  188.             break;
  189.         }
  190.     }
  191.     /* Assume it was at beginning */
  192.     fseek(fp,0L,SEEK_SET);
  193.     return rval;
  194. }
  195.  
  196.